package main

import (
	"fmt"
	"github.com/nsqio/go-nsq"
	"io"
	"log"
	"os"
)


func main() {

	conn := "39.100.58.10:4150"
	config := nsq.NewConfig()
	w, err := nsq.NewProducer(conn, config)

	f, err := os.OpenFile("nsqd.log", os.O_APPEND|os.O_WRONLY|os.O_CREATE, os.ModeAppend)
	if err != nil {
		fmt.Println("err", err)
	}
	w.SetLogger(log.New(io.MultiWriter(f), "", log.LstdFlags), nsq.LogLevelInfo)

	//w.SetLogger(log.New(ioutil.Discard, "", log.LstdFlags), nsq.LogLevelInfo)

	if err != nil {
		log.Panic(err)
	}

	chars := []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZ")

	// 同步发布将消息正文发布到指定的主题，如果发布失败，则返回错误
	//err = w.Publish("orderCreated", chars)
	//if err != nil {
	//	log.Panic(err)
	//}

	// 异步发送消息，不等待nsqd的响应
	err = w.PublishAsync("orderCreated", chars, nil)
	if err != nil {
		log.Panic(err)
	}

	// 延迟消息
	//err = w.DeferredPublish("orderCreated", 5 * time.Second, chars)
	//if err != nil {
	//	log.Panic(err)
	//}



	w.Stop()

}
